package com.qf.listener;

import com.netflix.hystrix.util.Exceptions;
import com.qf.constatns.RabbitMQConstatns;
import com.qf.entity.OrderDetailEntity;
import com.qf.entity.OrderEntity;
import com.qf.feign.api.OrderService;
import com.qf.service.StockService;
import com.qf.utils.ExecutorUtils;
import com.qf.vo.Order;
import com.qf.vo.StockParam;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rx.internal.util.ExceptionsUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Component
@Slf4j
public class StockDlxQueuelistener {

    private ThreadPoolExecutor exceptions = ExecutorUtils.getExcutorsPool();

    @Autowired
    private OrderService orderService;

    @Autowired
    private StockService stockService;

    @RabbitListener(queues = RabbitMQConstatns.STOCK_DLX_QUEUE)
    public void unlockStock(OrderEntity orderEntity, Channel channel, Message message) {
        exceptions.submit(() -> {
            log.debug("【订单要么支付了，要么取消了，要么超时了】库存马上就要释放了，根据订单的状态来做，{}", orderEntity);
            try {
                // 1.查询最新订单状态
                String oid = orderEntity.getOid();
                Order newStatus = orderService.getOrderInfoById(oid);
                log.debug("订单的最新状态:{}", newStatus);
                // 2.根据订单的状态进行库存释放
                switch (newStatus.getStatus()) {
                    case 1: // 未支付
                        System.out.println("2M了，订单还是未支付，释放库存");
                        unloackStock(newStatus);
                        break;
                    case 2: // 已支付
                        System.out.println("");
                        break;
                    case 3: // 已取消
                        System.out.println("2M了，订单已取消，释放库存");
                        unloackStock(newStatus);
                        break;
                    case 4: // 已超时了
                        System.out.println("2M了，订单超时了，释放库存");
                        unloackStock(newStatus);
                        break;

                }

                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }

    public void unloackStock(Order order) {

        List<StockParam> list = new ArrayList<>();

        // 1.根据订单编号查询订详情
        List<OrderDetailEntity> orderDetailEntityList = order.getOrderDetailEntityList();
        for (OrderDetailEntity orderDetailEntity : orderDetailEntityList) {

            Integer sum = orderDetailEntity.getSum();
            Integer gid = orderDetailEntity.getGid();

            // 释放库存
            StockParam stockParam = new StockParam();
            stockParam.setGid(gid);
            stockParam.setSum(sum);
            stockParam.setWid(1);

            list.add(stockParam);

        }

        // 释放库存
        stockService.unLockGoodsStock(list);
    }
}
